﻿using Serilog;

namespace Huioo.Learning.Serilog.ConsoleApp
{
    internal class Program
    {
        /// <summary>
        /// Serilog示例
        /// </summary>
        /// <param name="args"></param>
        static async Task Main(string[] args)
        {
            //通过 LoggerConfiguration 对象创建日志记录器
            //设置 Serilog 的全局、静态可访问日志记录器，后续调用 Log 类上的静态方法记录日志
            Log.Logger = new LoggerConfiguration()
                //基于控制台的接收器
                .WriteTo.Console()
                //基于文本的接收器
                .WriteTo.File("log.txt",
                    rollingInterval: RollingInterval.Day /* 按天生成文件，追加时间后缀 */,
                    rollOnFileSizeLimit: true /* 启用文件大小上限，追加序号后缀 */)
                .CreateLogger();

            try
            {
                // Your program here...
                const string name = "Serilog";
                Log.Information("Hello, {Name}!", name);
                throw new InvalidOperationException("Oops...");
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Unhandled exception");
            }
            finally
            {
                await Log.CloseAndFlushAsync(); // ensure all logs written before app exits
            }
        }
    }
}
